package com.feng.cloud.common.utils;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
import com.feng.cloud.common.exception.system.ParameterException;
import com.feng.cloud.common.exception.system.SystemStatus;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName LambdaUtils
 * @Description TODO
 * @Author Better Feng
 * @Date 2019/12/23 16:22
 * @Version 1.0
 **/
@UtilityClass
@Slf4j
public class LambdaUtils {

    public <T> String getProperty(SFunction<T, ?> func) {
        SerializedLambda lambda = com.baomidou.mybatisplus.core.toolkit.LambdaUtils.resolve(func);
        if (lambda == null) {
            return null;
        }
        // 获取方法名
        String methodName = lambda.getImplMethodName();
        String prefix = null;
        if(methodName.startsWith("get")){
            prefix = "get";
        }
        else if(methodName.startsWith("is")){
            prefix = "is";
        }
        if (StringUtils.isBlank(prefix)) {
            log.warn("无效的getter方法: " + methodName);
            throw new ParameterException(SystemStatus.INVALID_GETTER_METHOD);
        }
        return WebUtils.toLowerCaseFirstOne(methodName.replace(prefix, ""));
    }
}
